`
https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxLevelSum = function (root) {
  if (root === null) return 0

  const q = [root]
  let depth = 1
  let res = 0, maxSum = -Infinity

  while (q.length) {
    const sz = q.length
    let levelSum = 0
    for (let i = 0; i < sz; i++) {
      const cur = q.shift()
      levelSum += cur.val
      if (cur.left) q.push(cur.left)
      if (cur.right) q.push(cur.right)
    }
    if (levelSum > maxSum) {
      maxSum = levelSum
      res = depth
    }
    depth++
  }

  return res
};